package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;

    @Override
    public void deleteById(Long id) {
        //1.查询菜品
        Integer count1 = dishService.query().eq("category_id", id).count();
        if (count1 > 0) {
            throw new CustomerException("该分类下有关联的菜品，不能删除！");
        }
        //2.查询套餐
        Integer count2 = setmealService.query().eq("category_id", id).count();
        if (count2 > 0) {
            throw new CustomerException("该分类下有关联的套餐，不能删除！");
        }
        //3.删除分类
        removeById(id);
    }
}
